package com.huiztech.maystall.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MsgCenterActivity extends BaseActivity {

    private ListView listView;
    private List<Map<String, Object>> list;
    private ListAdapter adapter;

    private Button backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_center);

        backBtn = (Button) findViewById(R.id.msg_center_back_btn);
        backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        listView = (ListView) findViewById(R.id.list);
        list = new ArrayList<Map<String, Object>>();
        adapter = new ListAdapter(this);
        listView.setAdapter(adapter);

        getData();

    }

    private void getData() {
        for (int i = 0; i < 5; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("content", "Maecenas interdum feugiat leo , in suscipit");
            list.add(map);
        }
        adapter.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView contentTextView;
        ImageView deleteImage;
    }

    class ListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ListAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.msg_center_list_item, null);
                holder.contentTextView = (TextView) convertView.findViewById(R.id.msg_center_content);
                holder.deleteImage = (ImageView) convertView.findViewById(R.id.msg_center_delete_imageView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            holder.contentTextView.setText(list.get(position).get("content").toString());
            holder.deleteImage.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    adapter.notifyDataSetChanged();
                }
            });
            
            return convertView;

        }

    }
}
